package ae.pcfc.etks.online.itworx.ehs.web.common.dozer.convertors;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.dozer.DozerConverter;
import org.dozer.Mapper;
import org.dozer.MapperAware;

import ae.pcfc.etks.online.itworx.ehs.web.entity.FNPDetails;
import ae.pcfc.etks.online.itworx.ehs.web.entity.OFCRequest;
import ae.pcfc.etks.online.itworx.ehs.web.entity.Service;
import ae.pcfc.etks.online.itworx.ehs.web.enums.RequestStatus;
import ae.pcfc.etks.online.itworx.ehs.web.enums.RequestType;
import ae.pcfc.etks.online.itworx.ehs.web.enums.WorkflowDataFields;
import ae.pcfc.etks.online.itworx.ehs.web.util.UtilCommon;

public class OFCRequestConvertor extends DozerConverter<OFCRequest, Map> implements
		MapperAware {

	//TODO implement this for all attributes 
	private Mapper mapper;

	public OFCRequestConvertor() {
		super(OFCRequest.class, Map.class);
	}

	@Override
	public void setMapper(Mapper mapper) {
		this.mapper = mapper;

	}

	@Override
	public Map<String, String> convertTo(OFCRequest ofcRequest, Map mapData) {
		
		mapData.put(WorkflowDataFields.tradeLicenseNo.getPropertyName(), ofcRequest.getLicenseNumber());
		//mapData.put(WorkflowDataFields.activity.getPropertyName(), ofcRequest.getActivity());
		mapData.put(WorkflowDataFields.location.getPropertyName(), ofcRequest.getLocation());
		mapData.put(WorkflowDataFields.bcc.getPropertyName(), ofcRequest.getBcc());
		mapData.put(WorkflowDataFields.requestBy.getPropertyName(), ofcRequest.getRequestedBy());
		mapData.put(WorkflowDataFields.optionalMail.getPropertyName(), ofcRequest.getOptionalMail());
	    mapData.put(WorkflowDataFields.creationDate.getPropertyName(), ofcRequest.getCreationDate());
		mapData.put(WorkflowDataFields.contactPersonName.getPropertyName(), ofcRequest.getContactPersonName());
		mapData.put(WorkflowDataFields.fax.getPropertyName(), ofcRequest.getFax());
		mapData.put(WorkflowDataFields.imsLogID.getPropertyName(), ofcRequest.getImsLogId());
		mapData.put(WorkflowDataFields.mobile.getPropertyName(), ofcRequest.getContactPersonName());
		mapData.put(WorkflowDataFields.remark.getPropertyName(), ofcRequest.getRemarks());
		mapData.put(WorkflowDataFields.telephone.getPropertyName(), ofcRequest.getTelephone());
		mapData.put(WorkflowDataFields.mainMail.getPropertyName(), ofcRequest.getMainEmail());
		mapData.put(WorkflowDataFields.amendmentType.getPropertyName(), ofcRequest.getAmendmentType());
		mapData.put(WorkflowDataFields.status.getPropertyName(), ofcRequest.getStatus().getStatusName());
		mapData.put(WorkflowDataFields.imsStatus.getPropertyName(), ofcRequest.getImsStatus());
		mapData.put(WorkflowDataFields.customerUserID.getPropertyName(), ofcRequest.getUserId());
		mapData.put(WorkflowDataFields.officerID.getPropertyName(), ofcRequest.getOfficerId());
		mapData.put(WorkflowDataFields.requestID.getPropertyName(), Service.ofcService.getName() + "_" + ofcRequest.getType().name() + "_" + UtilCommon.getStringFromDate(ofcRequest.getCreationDate()) + "_" + ofcRequest.getId());
		mapData.put(WorkflowDataFields.requestType.getPropertyName(), ofcRequest.getType().name());
		mapData.put(WorkflowDataFields.companyName.getPropertyName(), ofcRequest.getCompanyNameEnglish());
		mapData.put(WorkflowDataFields.alternativeMail.getPropertyName(), ofcRequest.getAlternativeEmail());
		mapData.put(WorkflowDataFields.arabicActivity.getPropertyName(), ofcRequest.getArActivity());
		mapData.put(WorkflowDataFields.englishActivity.getPropertyName(), ofcRequest.getEngActivity());
		
		List<FNPDetails> fnpDetails = ofcRequest.getFnpList();
		if(null != fnpDetails && fnpDetails.size() >0){
			mapData.put(WorkflowDataFields.fees.getPropertyName(), fnpDetails.get(0).getFees());
			mapData.put(WorkflowDataFields.paymentAdvice.getPropertyName(), fnpDetails.get(0).getPaymentNumber());
			if(fnpDetails.size() >= 2){
				mapData.put(WorkflowDataFields.additionalFees.getPropertyName(), fnpDetails.get(1).getFees());
				mapData.put(WorkflowDataFields.additionalPaymentAdvice.getPropertyName(), fnpDetails.get(1).getPaymentNumber());
			}
		}
		
		return mapData;
	}

	@Override
	public OFCRequest convertFrom(Map mapData, OFCRequest ofcRequest) {
		
		if(mapData.containsKey(WorkflowDataFields.tradeLicenseNo.getPropertyName()))
		{
			ofcRequest.setLicenseNumber(mapData.get(WorkflowDataFields.tradeLicenseNo.getPropertyName()).toString());
		}
		
		/*if(mapData.containsKey(WorkflowDataFields.activity.getPropertyName()))
		{
			ofcRequest.setActivity(mapData.get(WorkflowDataFields.activity.getPropertyName()).toString());
		}*/
		
		if(mapData.containsKey(WorkflowDataFields.arabicActivity.getPropertyName()))
		{
			ofcRequest.setArActivity(mapData.get(WorkflowDataFields.arabicActivity.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.englishActivity.getPropertyName()))
		{
			ofcRequest.setEngActivity(mapData.get(WorkflowDataFields.englishActivity.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.location.getPropertyName()))
		{
			ofcRequest.setLocation(mapData.get(WorkflowDataFields.location.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.bcc.getPropertyName()))
		{
			ofcRequest.setBcc(mapData.get(WorkflowDataFields.bcc.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.requestBy.getPropertyName()))
		{
			ofcRequest.setRequestedBy(mapData.get(WorkflowDataFields.requestBy.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.optionalMail.getPropertyName()))
		{
			ofcRequest.setOptionalMail(mapData.get(WorkflowDataFields.optionalMail.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.creationDate.getPropertyName()))
		{
			ofcRequest.setCreationDate(new Date(mapData.get(WorkflowDataFields.creationDate.getPropertyName()).toString()));
		}
		
		if(mapData.containsKey(WorkflowDataFields.contactPersonName.getPropertyName()))
		{
			ofcRequest.setContactPersonName(mapData.get(WorkflowDataFields.contactPersonName.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.fax.getPropertyName()))
		{
			ofcRequest.setFax(mapData.get(WorkflowDataFields.fax.getPropertyName()).toString());
		}

		if(mapData.containsKey(WorkflowDataFields.imsLogID.getPropertyName()))
		{
			ofcRequest.setImsLogId(mapData.get(WorkflowDataFields.imsLogID.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.mobile.getPropertyName()))
		{
			ofcRequest.setMobile(mapData.get(WorkflowDataFields.mobile.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.remark.getPropertyName()))
		{
			ofcRequest.setRemarks(mapData.get(WorkflowDataFields.remark.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.alternativeMail.getPropertyName()))
		{
			ofcRequest.setAlternativeEmail(WorkflowDataFields.alternativeMail.getPropertyName());
		}
		
		if(mapData.containsKey(WorkflowDataFields.telephone.getPropertyName()))
		{
			ofcRequest.setTelephone(mapData.get(WorkflowDataFields.telephone.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.mainMail.getPropertyName()))
		{
			ofcRequest.setMainEmail(mapData.get(WorkflowDataFields.mainMail.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.amendmentType.getPropertyName()))
		{
			ofcRequest.setAmendmentType(mapData.get(WorkflowDataFields.amendmentType.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.status.getPropertyName()))
		{
			ofcRequest.setStatus(RequestStatus.valueOf(mapData.get(WorkflowDataFields.status.getPropertyName()).toString()));
		}
		
		if(mapData.containsKey(WorkflowDataFields.imsStatus.getPropertyName()))
		{
			ofcRequest.setImsStatus(mapData.get(WorkflowDataFields.imsStatus.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.customerUserID.getPropertyName()))
		{
			ofcRequest.setUserId(mapData.get(WorkflowDataFields.customerUserID.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.officerID.getPropertyName()))
		{
			ofcRequest.setOfficerId(mapData.get(WorkflowDataFields.officerID.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.requestID.getPropertyName()))
		{
			ofcRequest.setId(Long.parseLong(mapData.get(WorkflowDataFields.requestID.getPropertyName()).toString()));
		}
		
		if(mapData.containsKey(WorkflowDataFields.requestType.getPropertyName()))
		{
			ofcRequest.setType(RequestType.valueOf(mapData.get(WorkflowDataFields.requestType.getPropertyName()).toString()));
		}
		
		if(mapData.containsKey(WorkflowDataFields.companyName.getPropertyName()))
		{
			ofcRequest.setCompanyNameEnglish(mapData.get(WorkflowDataFields.companyName.getPropertyName()).toString());
		}
		
		if(mapData.containsKey(WorkflowDataFields.fees.getPropertyName()))
		{
			ofcRequest.getFnpList().get(0).setFees(Float.valueOf((mapData.get(WorkflowDataFields.fees.getPropertyName()).toString())));
		}
		
		if(mapData.containsKey(WorkflowDataFields.additionalFees.getPropertyName()))
		{
			ofcRequest.getFnpList().get(1).setFees(Float.valueOf((mapData.get(WorkflowDataFields.additionalFees.getPropertyName()).toString())));
		}
		
		if(mapData.containsKey(WorkflowDataFields.paymentAdvice.getPropertyName()))
		{
			ofcRequest.getFnpList().get(0).setPaymentNumber((mapData.get(WorkflowDataFields.paymentAdvice.getPropertyName()).toString()));
		}
		
		if(mapData.containsKey(WorkflowDataFields.additionalPaymentAdvice.getPropertyName()))
		{
			ofcRequest.getFnpList().get(1).setPaymentNumber((mapData.get(WorkflowDataFields.additionalPaymentAdvice.getPropertyName()).toString()));
		}
		return ofcRequest;
	}

}
